package v3.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import v3.object.STcfg;
import v3.object.command.iCommand;
import v3.object.command.list.Analysis;
import v3.object.command.list.Impulse;
import v3.object.command.list.Log;
import v3.object.command.list.LongSensor;
import v3.object.command.list.Manuver;
import v3.object.command.list.Shield;
import v3.object.command.list.ShortSensor;
import v3.object.command.list.ShowSector;
import v3.object.command.list.Warp;
import v3.object.command.servoCommand.ComputerMessage;
import v3.object.command.servoCommand.Help;
import v3.object.command.servoCommand.NoCommand;
import v3.object.ship.Ship;
import v3.object.space.Universe;


public class Engine
{

	protected Map<String, iCommand> commands;
	protected Map<String, String> shortcutCommand;

	protected int universeSize;
	protected Universe universe;
	protected Ship ship;
	private STcfg cfg;

	public Engine(Ship ship, Universe universe,  STcfg cfg)
	{
	
		this.cfg = cfg;
		this.universe = universe;
		this.ship=ship;
		commands = registerCommands();
		shortcutCommand = registerShortcutCommand();

	}


	public void engage() throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String input = "";
		while (!input.equalsIgnoreCase("exit"))
		{
			if(!ship.isAlive())
			{
				iCommand c  = new ComputerMessage(
						"*********************************************************\n"+
								" SHIP DISTROY"+
								" GAME OVER"+
						"*********************************************************\n");
				c.execute(cfg, universe, ship, commands, null);
				System.out.println("Good Bye");
				return;
				
			}
			System.out.print("command: -> ");
			input = br.readLine();
			executeCommand(input);
		}
		System.out.println("Good Bye");

	}

	private void executeCommand(String input)
	{
		int index=-1;
		try
		{
			index = Integer.parseInt(input);
			List<String> log = ship.getCptLongArray();
			executeCommand(log.get(index));
			return;
		}
		catch (Exception e)
		{
			//e.printStackTrace();
			//not a log Command
		}
		
		
		String[] parts = input.split(" ");
		iCommand command = commands.get(parts[0]);
		String cmd = shortcutCommand.get(parts[0]);

		if (cmd == null && command == null)
		{
			new NoCommand().execute(cfg, universe, ship, commands, parts);
			return;
		}

		if (command == null)
		{
			command = commands.get(cmd);
		}

		if (command == null)
		{
			new NoCommand().execute(cfg, universe, ship, commands, parts);
			return;
		}

		try
		{
			command.execute(cfg, universe, ship, commands, parts);
			ship.getCptLong().put(universe.starDate, input);
			ship.getCptLongArray().add(input);
		}
		catch (Exception e)
		{
			System.out.println("Error " + e);
		}

	}

	private Map<String, iCommand> registerCommands()
	{
		commands = new HashMap<String, iCommand>();
		commands.put("shortsensor", new ShortSensor());
		commands.put("analysis", new Analysis());
		commands.put("help", new Help());
		commands.put("impulse", new Impulse());
		commands.put("log", new Log());
		commands.put("shield", new Shield());
		commands.put("longsensor", new LongSensor());
		commands.put("warp", new Warp());
		commands.put("showsector", new ShowSector());
		commands.put("manuver", new Manuver());
		/*commands.put("help", new Help());
		commands.put("phaser", new Phaser());
		commands.put("dock", new Dock());
		commands.put("torpedo", new Thorpedo());
		commands.put("attack", new Attack());
		commands.put("attacksensor", new AttackSensor());
		commands.put("fakeattak", new FakeAttack());
		commands.put("enemymove", new EnemyMoveCommand());/**/
		
		return commands;
	}

	private Map<String, String> registerShortcutCommand()
	{
		shortcutCommand = new HashMap<String, String>();
		shortcutCommand.put("ss", "shortsensor");
		shortcutCommand.put("an", "analysis");
		shortcutCommand.put("h", "help");
		shortcutCommand.put("i", "impulse");
		shortcutCommand.put("log", "log");
		shortcutCommand.put("s", "shield");
		shortcutCommand.put("ls", "longsensor");
		shortcutCommand.put("w", "warp");
		shortcutCommand.put("shs", "showsector");		
		shortcutCommand.put("h", "help");
		shortcutCommand.put("m", "manuver");
		/*shortcutCommand.put("p", "phaser");
		shortcutCommand.put("t", "torpedo");
		shortcutCommand.put("d", "dock");
		shortcutCommand.put("a", "attack");
		shortcutCommand.put("as", "attacksensor");
		shortcutCommand.put("fa", "fakeattak");
		shortcutCommand.put("em", "enemymove");/**/
		
		return shortcutCommand;
	}

}
